// Copyright  rocedu@BESTI   rocedu@qq.com   All rights reserved.

 #include <stdio.h>
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
 #include <linux/limits.h>

 int main(int argc, char *argv[]) {
     // 瀹氫箟涓€涓瓧绗︽暟缁勭敤浜庡瓨鍌ㄦ枃浠跺悕锛屾渶澶ч暱搴︿负PATH_MAX
         char filename[PATH_MAX] = { 0 };

             // 妫€鏌ュ懡浠よ鍙傛暟鐨勬暟閲忥紝搴旇涓?锛堝寘鍚▼搴忓悕鍜屾枃浠跺悕锛?
                 if (argc != 2) {
                         fprintf(stderr, "You must supply a filename as an argument\n");
                                 return 1;  // 杩斿洖1琛ㄧず鍙傛暟閿欒
                                     }

                                         // 灏嗗懡浠よ鍙傛暟涓殑鏂囦欢鍚嶆嫹璐濆埌filename鏁扮粍涓紝纭繚涓嶈秴杩嘝ATH_MAX-1
                                             strncpy(filename, argv[1], sizeof(filename) - 1);
                                                 filename[sizeof(filename) - 1] = '\0';  // 纭繚瀛楃涓蹭互null缁撳熬

                                                     // 鍒涘缓鏂囦欢锛屾潈闄愯缃负鍙鍙啓锛?644锛?
                                                         if (creat(filename, 0644) == -1) {
                                                                 fprintf(stderr, "Can't create file %s \n", filename);

                                                                         // 閿欒澶勭悊锛屾牴鎹甧rrno鐨勫€兼墦鍗扮浉搴旂殑閿欒淇℃伅
                                                                                 if (errno == EACCES) {
                                                                                             fprintf(stderr, "Permission denied ");
                                                                                                     } else if (errno == ENOENT) {
                                                                                                                 fprintf(stderr, "Parent directories do not exist \n");
                                                                                                                         } else {
                                                                                                                                     fprintf(stderr, "Unknown error: %s \n", strerror(errno));  // 浣跨敤strerror鎻愪緵鍏蜂綋鐨勯敊璇俊鎭?
                                                                                                                                             }

                                                                                                                                                     return 1;  // 杩斿洖1琛ㄧず鏂囦欢鍒涘缓澶辫触
                                                                                                                                                         }

                                                                                                                                                             return 0;  // 杩斿洖0琛ㄧず绋嬪簭鎴愬姛鎵ц
                                                                                                                                                             }

